<?php
/**
 * Created by PhpStorm.
 * User: bg
 * Date: 2017/8/25 0025
 * Time: 下午 4:08
 */
namespace app\common\model;

use think\Model;
use \think\Db;
use \think\Loader;
/*
 * 项目配置模型
 */
class Configuration extends Model
{
    //添加配置数据
    public function add($value){
        $data = $value;
        $ret = $this->insertGetId($data);
        return $ret;
    }
    //根据key获取json值
    public function getkeyjson($key){
        $value = db('configuration')->where(array('name'=>$key))->find();
        if(!$value){
            return false;
        }
        return json_decode($value['value']);
    }

    //根据key获取值
    public function getkey($key){
        $value = $this->where(array('name'=>$key))->find();
        if(!$value){
            return false;
        }
        return $value->data['value'];
    }

    //根据key判断是否存在
    public function getIskey($key){
        $value = $this->where(array('name'=>$key))->find();
        if(!$value){
            return false;
        }
        return true;
    }


    //获取配置表所有数据
    public function getlist(){
        $retArray = array();
        $value = $this->select();
        foreach ($value as $key=>$val){
            $retArray[$val->data['name']] = json_decode($val->data['value']);;
        }
        return $retArray;
    }

    //根据key修改数据
    public function updatekey($key,$value){
        $map['name'] = $key;
        $ret = $this->where($map)->update(array('value'=>$value));
        return $ret;
    }

    //根据key删除数据
    public function delkey($key){
        $map['name'] = $key;
        $ret = $this->where($map)->delete();
        return $ret;
    }
    //修改
    public function updateData($name,$param){
        $map['name'] = $name;
        $ret = $this->where($map)->update($param);

        return $ret;
    }
}
